package lium.lbs.lbsim.activity

import android.content.Intent
import android.graphics.Color
import android.os.Bundle
import android.view.View
import com.buz.hjcdriver.bean.FristLurnchBean
import com.lmlibrary.Constants
import com.lmlibrary.bean.ResponseBean
import com.lzy.okgo.model.Response
import kotlinx.android.synthetic.main.activity_forget_password.*
import lium.lbs.lbsim.R
import lium.lbs.lbsim.jgchat.utils.photovideo.takevideo.utils.LogUtils
import lium.lbs.lbsim.utils.CircleProgressbar
import lium.lbs.lbsim.utils.SpUtils
import lium.lbs.lbsim.utils.callbck.JsonCallback
import java.util.*

class LauncherActivity : BaseActivity() {
    override fun getLayoutId(): Int {
        return R.layout.activity_forget_password
    }


    override fun initView(savedInstanceState: Bundle?) {
    }

    override fun getNetWorkData() {
    }

    private var isLogin: Boolean = false
    private var mCircleProgressbar: CircleProgressbar? = null
    var timeNow: Int = 5
    var timer = Timer()

    internal var task: TimerTask = object : TimerTask() {
        override fun run() {
            runOnUiThread {
                timeNow--
                tv_red_skip.text = timeNow.toString()

                if (timeNow <= 0) {
                    timer.cancel();


                }

            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (intent.flags and Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT != 0) {
            finish()
            return
        }
        isLogin = SpUtils.getInstance().get(Constants.isLogin, false) as Boolean
        getSysteMessages()
    }

    fun getSysteMessages() {
        val parrmt = HashMap<String, String>()
        postData<ResponseBean<FristLurnchBean>>(
            "/common/getStartPageInfo",
            parrmt,
            object : JsonCallback<ResponseBean<FristLurnchBean>>(this) {
                override fun onSuccess(response: Response<ResponseBean<FristLurnchBean>>) {
                    var miaoshu = Integer.parseInt(response.body().data.miaoshu)
                    timer.schedule(task, 0, 1500);
                    mCircleProgressbar = findViewById<View>(R.id.tv_red_skip) as CircleProgressbar
                    mCircleProgressbar!!.setOutLineColor(Color.TRANSPARENT)
                    mCircleProgressbar!!.setInCircleColor(Color.parseColor("#70000000"))
                    mCircleProgressbar!!.setProgressColor(Color.parseColor("#E21E1A"))
                    mCircleProgressbar!!.setProgressLineWidth(6)
                    mCircleProgressbar!!.setProgressType(CircleProgressbar.ProgressType.COUNT_BACK)
                    mCircleProgressbar!!.setTimeMillis(5000)
                    mCircleProgressbar!!.setProgress(miaoshu);
                    mCircleProgressbar!!.reStart()
                    mCircleProgressbar!!.setCountdownProgressListener(1, progressListener)
                    mCircleProgressbar!!.setOnClickListener(View.OnClickListener {
                        val intent: Intent = if (isLogin) {
                            Intent(this@LauncherActivity, MainActivity::class.java)
                        } else {
                            Intent(this@LauncherActivity, LoginActivity::class.java)
                            //   Intent(this@LauncherActivity, WXPayActivity::class.java)
                        }
                        startActivity(intent)
                        finish()
                    })
                    displayImage(response.body().data.imgList.get(0), R.drawable.icon_myqidong, show_pic)
                }
            })


    }


    private val progressListener = object : CircleProgressbar.OnCountdownProgressListener {
        override fun onProgress(what: Int, progress: Int) {
            LogUtils.e("body" + progress)

            if (what == 1 && progress == 0) {
                mCircleProgressbar!!.text = progress.toString()
                val intent: Intent = if (isLogin) {
                    Intent(this@LauncherActivity, MainActivity::class.java)
                } else {
                    Intent(this@LauncherActivity, LoginActivity::class.java)
                    //   Intent(this@LauncherActivity, WXPayActivity::class.java)
                }
                startActivity(intent)
                finish()
            } else if (what == 2) {
                mCircleProgressbar!!.text = progress.toString()
            }

        }
    }

}
